<?php
namespace Modules\Home\Controllers;

class UserWalletCouponLogController extends ControllerBase {
  public function initialize() {
    parent::initialize();

    $this -> _checkLogin();
    $this -> view -> setLayout('index');
  }

  public function indexAction() {
    $this -> tag -> prependTitle('购物券记录');

    $currentPage = abs($this -> request -> getQuery('page', 'int', 1));
    if($currentPage <= 0) {
      $currentPage = 1;
    }

    $sessionGetUser = $this -> session -> get('user');

    $builderUserWalletCouponLog = $this -> modelsManager -> createBuilder()
      -> from([
        'uwcl' => 'Common\\Models\\UserWalletCouponLogModel'
      ])
      -> where('uwcl.user_id = ' . $sessionGetUser['user_id'])
      -> columns(
        'uwcl.user_wallet_coupon_log_id, uwcl.type, uwcl.active, uwcl.create_time, uwcl.amount, uwcl.amount_new, uwcl.amount_old'
      )
      -> orderBy('uwcl.user_wallet_coupon_log_id DESC');

    $pagerUserWalletCouponLog = new \Phalcon\Paginator\Pager(new \Phalcon\Paginator\Adapter\QueryBuilder([
      'builder' => $builderUserWalletCouponLog,
      'limit' => 10,
      'page' => $currentPage
    ]), [
      'layoutClass' => 'Modules\\Home\\Libraries\\Paginator\\Pager\\Layout\\Bootstrap',
      'rangeLength' => 5,
      'urlMask' => '?page={%page_number}'
    ]);

    $this -> view -> setVar('pagerUserWalletCouponLog', $pagerUserWalletCouponLog);
  }
}
?>